
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>javase04 多线程开始 · JavaSe 基础</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="<yangxin19970404@163.com>">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="javase05 静态代理和Lambda表达式.html" />
    
    
    <link rel="prev" href="javase03 HashMap总结.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="javase01 HashMap基础.html">
            
                <a href="javase01 HashMap基础.html">
            
                    
                    javase01 HashMap基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="javase02 HashMap构造方法分析.html">
            
                <a href="javase02 HashMap构造方法分析.html">
            
                    
                    javase02 HashMap构造方法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="javase03 HashMap总结.html">
            
                <a href="javase03 HashMap总结.html">
            
                    
                    javase03 HashMap总结
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5" data-path="javase04 多线程开始.html">
            
                <a href="javase04 多线程开始.html">
            
                    
                    javase04 多线程开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="javase05 静态代理和Lambda表达式.html">
            
                <a href="javase05 静态代理和Lambda表达式.html">
            
                    
                    javase05 静态代理和Lambda表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="javase06 解决个编译错误.html">
            
                <a href="javase06 解决个编译错误.html">
            
                    
                    javase06 解决个编译错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="javase07 线程停止.html">
            
                <a href="javase07 线程停止.html">
            
                    
                    javase07 线程停止
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="javase08 线程休眠.html">
            
                <a href="javase08 线程休眠.html">
            
                    
                    javase08 线程休眠
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="javase09 线程礼让与强制执行.html">
            
                <a href="javase09 线程礼让与强制执行.html">
            
                    
                    javase09 线程礼让与强制执行
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="javase10 线程优先级.html">
            
                <a href="javase10 线程优先级.html">
            
                    
                    javase10 线程优先级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="javase11 守护线程.html">
            
                <a href="javase11 守护线程.html">
            
                    
                    javase11 守护线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="javase12 线程同步.html">
            
                <a href="javase12 线程同步.html">
            
                    
                    javase12 线程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="javase13 死锁.html">
            
                <a href="javase13 死锁.html">
            
                    
                    javase13 死锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="javase14 Lock锁.html">
            
                <a href="javase14 Lock锁.html">
            
                    
                    javase14 Lock锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="javase15 解决线程通信的问题.html">
            
                <a href="javase15 解决线程通信的问题.html">
            
                    
                    javase15 解决线程通信的问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="javase16 线程池.html">
            
                <a href="javase16 线程池.html">
            
                    
                    javase16 线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="javase17 File类.html">
            
                <a href="javase17 File类.html">
            
                    
                    javase17 File类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="javase18 IO流原理及字符流输入输出.html">
            
                <a href="javase18 IO流原理及字符流输入输出.html">
            
                    
                    javase18 IO流原理及字符流输入输出
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="javase19 字节输入输出流.html">
            
                <a href="javase19 字节输入输出流.html">
            
                    
                    javase19 字节输入输出流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="javase20 缓冲流.html">
            
                <a href="javase20 缓冲流.html">
            
                    
                    javase20 缓冲流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="javase21 转换流.html">
            
                <a href="javase21 转换流.html">
            
                    
                    javase21 转换流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="javase22 打印流.html">
            
                <a href="javase22 打印流.html">
            
                    
                    javase22 打印流
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >javase04 多线程开始</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x591A;&#x7EBF;&#x7A0B;">&#x591A;&#x7EBF;&#x7A0B;</h2>
<ul>
<li><p>&#x8FDB;&#x7A0B;&#x662F;&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x7684;&#x4E00;&#x6B21;&#x6267;&#x884C;&#x8FC7;&#x7A0B;,&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x52A8;&#x6001;&#x7684;&#x6982;&#x5FF5;,&#x662F;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x5206;&#x914D;&#x7684;&#x5355;&#x5143;</p>
</li>
<li><p>&#x901A;&#x5E38;&#x5728;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x4E2D;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x82E5;&#x5E72;&#x4E2A;&#x7EBF;&#x7A0B;,&#x5F53;&#x7136;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x4E2D;&#x81F3;&#x5C11;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;,&#x4E0D;&#x7136;&#x6CA1;&#x6709;&#x5B58;&#x5728;&#x7684;&#x610F;&#x4E49;.&#x7EBF;&#x7A0B;&#x65F6;CPU&#x8C03;&#x5EA6;&#x548C;&#x6267;&#x884C;&#x7684;&#x5355;&#x4F4D;</p>
</li>
</ul>
<p><strong>&#x6838;&#x5FC3;&#x6982;&#x5FF5;</strong>
<img src="./imgs/&#x5FAE;&#x4FE1;&#x622A;&#x56FE;_20210316135118.png" alt=""></p>
<h3 id="&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;--thread--runnable--callable">&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;  Thread  Runnable  Callable</h3>
<p>   Thread    &gt; &#x7EE7;&#x627F;Thread&#x7C7B;</p>
<pre><code class="lang-java">    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">//run&#x65B9;&#x6CD5;&#x7EBF;&#x7A0B;&#x4F53;</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span> ; i &lt; <span class="hljs-number">10</span>; i++){
            System.out.println(<span class="hljs-string">&quot;run&#x65B9;&#x6CD5;&#x7EBF;&#x7A0B;&#x4F53;...&quot;</span>);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-comment">//main&#x4E3B;&#x7EBF;&#x7A0B;</span>
        ThreadTest threadTest = <span class="hljs-keyword">new</span> ThreadTest();
        threadTest.start();

        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span> ; i &lt; <span class="hljs-number">100</span>; i++){
            System.out.println(<span class="hljs-string">&quot;&#x5B66;&#x4E60;&#x591A;&#x7EBF;&#x7A0B;...&quot;</span>);
        }
    }
</code></pre>
<ul>
<li>&#x5B50;&#x7C7B;&#x7EE7;&#x627F;Thread&#x7C7B;&#x5177;&#x5907;&#x591A;&#x7EBF;&#x7A0B;&#x7684;&#x80FD;&#x529B;</li>
<li>&#x542F;&#x52A8;&#x7EBF;&#x7A0B;:&#x5B50;&#x7C7B;&#x5BF9;&#x8C61;.start()</li>
<li><p><strong>&#x4E0D;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;: &#x907F;&#x514D;OOP&#x5355;&#x7EE7;&#x627F;&#x5C40;&#x9650;&#x6027;</strong></p>
<p>Runnable  &gt; &#x5B9E;&#x73B0;Runnable&#x63A5;&#x53E3;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
   <span class="hljs-comment">//&#x521B;&#x5EFA;Runnable&#x63A5;&#x53E3;&#x7684;&#x5B9E;&#x73B0;&#x5BF9;&#x8C61;</span>
   ThreadTest2 threadTest2 = <span class="hljs-keyword">new</span> ThreadTest2();
   <span class="hljs-comment">//&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x5BF9;&#x8C61;,&#x901A;&#x8FC7;&#x7EBF;&#x7A0B;&#x5BF9;&#x8C61;&#x6765;&#x5F00;&#x542F;&#x6211;&#x4EEC;&#x7684;&#x7EBF;&#x7A0B;,&#x4F7F;&#x7528;&#x4EE3;&#x7406;</span>
   Thread thread = <span class="hljs-keyword">new</span> Thread(threadTest2);
   thread.start();

   <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span> ; i &lt; <span class="hljs-number">100</span>; i++){
       System.out.println(<span class="hljs-string">&quot;&#x5B66;&#x4E60;&#x591A;&#x7EBF;&#x7A0B;...&quot;</span>);
   }
}

<span class="hljs-meta">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
   <span class="hljs-comment">//run&#x65B9;&#x6CD5;&#x7EBF;&#x7A0B;&#x4F53;</span>
   <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span> ; i &lt; <span class="hljs-number">10</span>; i++){
       System.out.println(<span class="hljs-string">&quot;run&#x65B9;&#x6CD5;&#x7EBF;&#x7A0B;&#x4F53;...&quot;</span>);
   }
}
</code></pre>
</li>
<li>&#x5B9E;&#x73B0;Runnable&#x63A5;&#x53E3;&#x5177;&#x6709;&#x591A;&#x7EBF;&#x7A0B;&#x7684;&#x80FD;&#x529B;</li>
<li>&#x542F;&#x52A8;&#x7EBF;&#x7A0B;:&#x4F20;&#x5165;&#x76EE;&#x6807;&#x5BF9;&#x8C61;+Thread&#x5BF9;&#x8C61;.start()</li>
<li><p><strong>&#x63A8;&#x8350;&#x4F7F;&#x7528;:&#x907F;&#x514D;&#x5355;&#x7EE7;&#x627F;&#x5C40;&#x9650;&#x6027;,&#x7075;&#x6D3B;&#x65B9;&#x4FBF;,&#x65B9;&#x4FBF;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x88AB;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x4F7F;&#x7528;</strong></p>
<p>Callable  &gt; &#x5B9E;&#x73B0;Callable&#x63A5;&#x53E3;</p>
</li>
<li>&#x5B9E;&#x73B0;Callable&#x63A5;&#x53E3;,&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;</li>
<li>&#x91CD;&#x5199;call&#x65B9;&#x6CD5;,&#x9700;&#x8981;&#x629B;&#x51FA;&#x5F02;&#x5E38;</li>
<li>&#x521B;&#x5EFA;&#x76EE;&#x6807;&#x5BF9;&#x8C61;</li>
<li>&#x521B;&#x5EFA;&#x6267;&#x884C;&#x670D;&#x52A1;:ExecutorService ser = Executors.newFixThreadPool(1); //&#x7EBF;&#x7A0B;&#x6C60;</li>
<li>&#x63D0;&#x4EA4;&#x6267;&#x884C;:Future<boolean> result1 = ser.submit(t1);</boolean></li>
<li>&#x83B7;&#x53D6;&#x7ED3;&#x679C;:boolean r1 = result1.get();</li>
<li>&#x5173;&#x95ED;&#x670D;&#x52A1;:ser.shutdownNow();</li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="javase03 HashMap总结.html" class="navigation navigation-prev " aria-label="Previous page: javase03 HashMap总结">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="javase05 静态代理和Lambda表达式.html" class="navigation navigation-next " aria-label="Next page: javase05 静态代理和Lambda表达式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"javase04 多线程开始","level":"1.5","depth":1,"next":{"title":"javase05 静态代理和Lambda表达式","level":"1.6","depth":1,"path":"javase05 静态代理和Lambda表达式.md","ref":"javase05 静态代理和Lambda表达式.md","articles":[]},"previous":{"title":"javase03 HashMap总结","level":"1.4","depth":1,"path":"javase03 HashMap总结.md","ref":"javase03 HashMap总结.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"<yangxin19970404@163.com>","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"JavaSe 基础","language":"zh-hans","gitbook":"3.2.3","description":"JavaSe 基础"},"file":{"path":"javase04 多线程开始.md","mtime":"2021-03-16T13:52:39.761Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-04-17T08:15:36.913Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

